#ifndef MEMORYDATAINITDEINIT_H_
#define MEMORYDATAINITDEINIT_H_


// 二分查找、并更新索引
// 参数:index   引用内存数组序号
// 参数:size    索引当前大小
// 参数:idx     索引
// 参数:dataIdx 待更新索引数据的数组序号
// 参数:data    待更新索引数据
// 参数:field   索引对应排序字段
// 参数:mode    索引处理类型（使用MODE_NONE/INSERT/DELETE宏）
template<typename T>
int __binarysearch(T * object, int index, int size, int* idx, int dataIdx,void * data, int field, char mode) {
	if (size == 0) {
		if (mode == MODE_INSERT)
			idx[0] = dataIdx;
		return NOT_FOUND; // 如果当前大小为空肯定无匹配
	}
	int low = 0; // 二分查找低值
	int high = size - 1; // 二分查找高值
	int middle = 0;
	int flag = NOT_FOUND_LEFT;
	void* _data;
	if (data == NULL && mode != MODE_INSERT)
		data = object->data(dataIdx);
	while (low <= high) {
		middle = (low + high) / 2;
		_data = object->data(idx[middle]);
		if (_compare_lt[index](_data, data, field)) {
			low = middle + 1;
			flag = NOT_FOUND_RIGHT;
		}
		else if (_compare_gt[index](_data, data, field)) {
			high = middle - 1;
			flag = NOT_FOUND_LEFT;
		}
		else {
			if (mode == MODE_DELETE) {
				for (int i = middle; i < size - 1; i++)
					idx[i] = idx[i + 1];
			}
			return middle;
		}
	}
	if (mode == MODE_INSERT) {
		if (flag == NOT_FOUND_RIGHT)
			middle++;
		// 注意这里插入，必须倒序更新、后面数据赋成前面值
		for (int i = size; i > middle; i--)
			idx[i] = idx[i - 1];
		idx[middle] = dataIdx;
	}
	return NOT_FOUND;
}
// 查询列表快速排序整理
template<typename T>
static void __quicksort(T * object, int index, int* indexs, int left, int right, int field) {
	//此处编写代码实现快速排序
	int i, j, middle, temp;
	if (left < right) {
		i = left;
		j = right;
		middle = indexs[(left + right) / 2];
		//以中间元素为轴
		while (true) {
			while (i <= right && _compare_lt[index](object->data(indexs[i]), object->data(middle), field))
				i++;
			while (j >= 0 && _compare_gt[index](object->data(indexs[j]), object->data(middle), field))

				j--;
			if (i >= j) //相遇则跳出
				break;
			else {
				temp = indexs[i];
				indexs[i] = indexs[j];
				indexs[j] = temp;
			}
		}
		__quicksort(object, index, indexs, left, i - 1, field); //对左半部分进行快排
		__quicksort(object, index, indexs, j + 1, right, field); //对右半部分进行快排
	}
}

class Orders {
public:
    Orders(int maxCount):_maxCount(maxCount){
        init();
    }
    ~Orders(){
        deinit();
    }
    int maxCount() const {
        return _maxCount;
    }
	int & count() {
		return _count;
	}
	void reset(){
		_count = 0;
		memset(arrayBaoDan, 0x00, sizeof(SBAODAN) * _maxCount);
		memset(_idx_baodan_O00, 0x00, sizeof(int) * _maxCount);
		memset(_idx_baodan_T12, 0x00, sizeof(int) * _maxCount);
		memset(_idx_filter_baodan_O00, 0x00, sizeof(int) * _maxCount);
		memset(_idx_filter_baodan_T12, 0x00, sizeof(int) * _maxCount);
	}
	SBAODAN * array(){
		return arrayBaoDan;
	}
    int * idx_baodan_O00(){
        return _idx_baodan_O00;
    }
    int * idx_baodan_T12(){
        return _idx_baodan_T12;
    }
    int * idx_filter_baodan_O00(){
        return _idx_filter_baodan_O00;
    }
    int * idx_filter_baodan_T12() {
        return _idx_filter_baodan_T12;
    }
    SFILTER_BAODAN & filter_baodan() {
        return _filter_baodan;
    }

	SBAODAN * data(int index){
		if(index >= 0 && index < _maxCount){
			return &(arrayBaoDan[index]);
		}
		return nullptr;
	}
	boost::shared_mutex & mutex(){
		return _mutex;
	}
    bool bIsFiltered() {
        return _bIsFiltered;
    }
    void bIsFiltered(bool filter){
        _bIsFiltered = filter;
    }
    int & iFilterCount() {
        return _iFilterCount;
    }
    boost::mutex & _index_mutexs() {
        return __index_mutexs;
    }

    int insertOrUpdate(void * data) {
		_mutex.lock();
        
        SBAODAN * baodan = (SBAODAN*)data;
		baodan->iT12_part = baodan->iT12 >= NIGHT_BEGIN_TIME ? baodan->iT12 - NIGHT_BEGIN_TIME : baodan->iT12 + DAY_BEGIN_OFFSET;
		baodan->sM20_part = baodan->iM20 / 100; // 席位号前4位的会员代码
		int idx = __binarysearch(this, INDEX_BAODAN, _count
				, _idx_baodan_O00, _count
				, data, FIELD_BAODAN_NONE
				, _count >= _maxCount ? MODE_NONE : MODE_INSERT);
		if (idx >= 0) {
			if (baodan->iX05 > this->data(_idx_baodan_O00[idx])->iX05) {
				bool flag1 = _bIsFiltered
					&& _matchfilter_baodan(&_filter_baodan, this->data(_idx_baodan_O00[idx]));
				memcpy(this->data(_idx_baodan_O00[idx]), baodan, sizeof(SBAODAN));
				// 数据发生更新、重新判断是否符合筛选条件；如果符合要从对应筛选索引中删除
				if (_bIsFiltered) {
					bool flag2 = _matchfilter_baodan(&_filter_baodan, baodan);
					if (flag1 && !flag2) {
						__binarysearch(this, INDEX_BAODAN, _iFilterCount, _idx_filter_baodan_O00, _idx_baodan_O00[idx], NULL, FIELD_BAODAN_NONE, MODE_DELETE);
						__binarysearch(this, INDEX_BAODAN, _iFilterCount, _idx_filter_baodan_T12, _idx_baodan_O00[idx], NULL, FIELD_BAODAN_iT12_part, MODE_DELETE);
						_iFilterCount --;
					}
					else if (!flag1 && flag2) {
						__binarysearch(this, INDEX_BAODAN, _iFilterCount, _idx_filter_baodan_O00, _idx_baodan_O00[idx], data, FIELD_BAODAN_NONE, MODE_INSERT);
						__binarysearch(this, INDEX_BAODAN, _iFilterCount, _idx_filter_baodan_T12, _idx_baodan_O00[idx], data, FIELD_BAODAN_iT12_part, MODE_INSERT);
						_iFilterCount ++;
					}
				}
			}
		}
		else {
			// 插入数据（判断是否超过最大值作越界容错/丢弃，注意如果换成从0开始覆盖算法，则需要从原各索引中找到覆盖序号删除并重新排序插入）
			if (_count >= _maxCount) {
				logs(error_,"this' count has been equal to the max_order_cnt, orderNo:" + to_string(baodan->iO00));
				_mutex.unlock();
				return -1;
			}
			memcpy(this->data(_count), baodan, sizeof(SBAODAN));
			// 更新其它索引以及筛选索引
			__binarysearch(this, INDEX_BAODAN, _count
					, _idx_baodan_T12, _count
					, data, FIELD_BAODAN_iT12_part, MODE_INSERT);
			if (_bIsFiltered && _matchfilter_baodan(&_filter_baodan, baodan)) {
				__binarysearch(this, INDEX_BAODAN, _iFilterCount
						, _idx_filter_baodan_O00, _count
						, data, FIELD_BAODAN_NONE, MODE_INSERT);
				__binarysearch(this, INDEX_BAODAN, _iFilterCount
						, _idx_filter_baodan_T12, _count
						, data, FIELD_BAODAN_iT12_part, MODE_INSERT);
				_iFilterCount ++;
			}
			_count++;
		}
		// 更新X05值
		//TODO: _update_x05(baodan->iX05);

		_mutex.unlock();
        return 0;
    }
private:
    int  _iFilterCount = 0;
    SFILTER_BAODAN _filter_baodan; //Init ???
	SBAODAN* arrayBaoDan = nullptr; // 报单列表
    int* _idx_baodan_O00 = nullptr;  // O00索引
    int* _idx_baodan_T12 = nullptr;  // T12+O00索引
    int* _idx_filter_baodan_O00 = nullptr; // 满足筛选O00索引
    int *_idx_filter_baodan_T12 = nullptr; // 满足筛选T12+O00索引

    bool _bIsFiltered = false;
    int _maxCount = 0; 
	int _count = 0;
	boost::shared_mutex _mutex;
    boost::mutex __index_mutexs; // 索引访问锁（非需要序列化索引）
private:
    void init(){
        arrayBaoDan = new SBAODAN[_maxCount];
        _idx_baodan_O00 = new int[_maxCount];
        _idx_baodan_T12 = new int[_maxCount];
        _idx_filter_baodan_O00 = new int[_maxCount];
        _idx_filter_baodan_T12 = new int[_maxCount];

		_filter_baodan.cI10 = -1;
		_filter_baodan.cO09 = -1;
		_filter_baodan.cO15 = -1;
		_filter_baodan.iM20 = -1;
		_filter_baodan.iM30 = -1;
    }
    void deinit(){
        if (arrayBaoDan) {
            delete [] arrayBaoDan;
            arrayBaoDan = nullptr;
        }
        if (_idx_baodan_O00) {
            delete [] _idx_baodan_O00;
            _idx_baodan_O00 = nullptr;
        }
        if (_idx_baodan_T12) {
            delete [] _idx_baodan_T12;
            _idx_baodan_T12 = nullptr;
        }
        if (_idx_filter_baodan_O00) {
            delete [] _idx_filter_baodan_O00;
            _idx_filter_baodan_O00 = nullptr;
        }
        if (_idx_filter_baodan_T12) {
            delete [] _idx_filter_baodan_T12;
            _idx_filter_baodan_T12 = nullptr;
        }
    }
};

class ContractNotes{
public:
    ContractNotes(int maxCount): _maxCount(maxCount){
        init();
    }
    ~ContractNotes(){
        deinit();
    }
    int maxCount() const {
        return _maxCount;
    }
	void reset(){
		_count = 0;
		memset(arrayChengJiaoDan, 0x00, sizeof(SCHENGJIAODAN) * _maxCount);
		memset(_idx_chengjiaodan_O60_O02, 0x00, sizeof(int) *  _maxCount);
		memset(_idx_chengjiaodan_O62, 0x00, sizeof(int) *  _maxCount);
		memset(_idx_filter_chengjiaodan_O60_O02, 0x00, sizeof(int) *  _maxCount);
		memset(_idx_filter_chengjiaodan_O62, 0x00, sizeof(int) *  _maxCount);
	}
	SCHENGJIAODAN * array(){
		return arrayChengJiaoDan;
	}
	SCHENGJIAODAN * data(int index){
		if(index >= 0 && index < _maxCount){
			return &(arrayChengJiaoDan[index]);
		}
		return nullptr;
	}
	boost::shared_mutex & mutex(){
		return _mutex;
	}
	int & count() {
		return _count;
	}


    SFILTER_CHENGJIAODAN & filter_chengjiaodan(){
        return _filter_chengjiaodan;
    }
    int * idx_chengjiaodan_O60_O02(){
        return _idx_chengjiaodan_O60_O02;
    }
    int * idx_chengjiaodan_O62() {
        return _idx_chengjiaodan_O62;
    }
    int * idx_filter_chengjiaodan_O60_O02(){
        return _idx_filter_chengjiaodan_O60_O02;
    }
    int * idx_filter_chengjiaodan_O62(){
        return _idx_filter_chengjiaodan_O62;
    }
    bool bIsFiltered() {
        return _bIsFiltered;
    }
    void bIsFiltered(bool filter){
        _bIsFiltered = filter;
    }
    int & iFilterCount() {
        return _iFilterCount;
    }
    boost::mutex & _index_mutexs() {
        return __index_mutexs;
    }
private:
    void init(){
        arrayChengJiaoDan = new SCHENGJIAODAN[_maxCount];
        _idx_chengjiaodan_O60_O02 = new int[_maxCount];
        _idx_chengjiaodan_O62 = new int[_maxCount];
        _idx_filter_chengjiaodan_O60_O02 = new int[_maxCount];
        _idx_filter_chengjiaodan_O62 = new int[_maxCount];

		_filter_chengjiaodan.cI10 = -1;
		_filter_chengjiaodan.iM20 = -1;
		_filter_chengjiaodan.iM30 = -1;

    }
    void deinit(){
        if (arrayChengJiaoDan) {
            delete [] arrayChengJiaoDan;
            arrayChengJiaoDan = nullptr;
        }
        if (_idx_chengjiaodan_O60_O02) {
            delete []_idx_chengjiaodan_O60_O02;
            _idx_chengjiaodan_O60_O02 = nullptr;
        }
        if (_idx_chengjiaodan_O62) {
            delete [] _idx_chengjiaodan_O62;
            _idx_chengjiaodan_O62 = nullptr;
        }
        if (_idx_filter_chengjiaodan_O60_O02) {
            delete [] _idx_filter_chengjiaodan_O60_O02;
            _idx_filter_chengjiaodan_O60_O02 = nullptr;
        }
        if (_idx_filter_chengjiaodan_O62) {
            delete [] _idx_filter_chengjiaodan_O62;
            _idx_filter_chengjiaodan_O62 = nullptr;
        }
    }
private:
    int _maxCount = 0;

	SCHENGJIAODAN* arrayChengJiaoDan = nullptr; // 成交单列表
    int *_idx_chengjiaodan_O60_O02 = nullptr;  // O60+O02索引
    int *_idx_chengjiaodan_O62 = nullptr;      // O62+O60+O02索引
    int *_idx_filter_chengjiaodan_O60_O02 = nullptr; // 满足筛选O60+O02索引
    int *_idx_filter_chengjiaodan_O62 = nullptr;     // 满足筛选O62+O60+O02索引

	boost::shared_mutex _mutex;
    boost::mutex __index_mutexs; // 索引访问锁（非需要序列化索引）

    SFILTER_CHENGJIAODAN _filter_chengjiaodan;
    bool _bIsFiltered = false;
    int  _iFilterCount = 0;

	int _count = 0;
};

class DeliveryOrders {
public:
    DeliveryOrders(int maxCount):_maxCount(maxCount){
        init();
    }
    ~DeliveryOrders(){
        deinit();
    }
    int maxCount() const {
        return _maxCount;
    }
	void reset() {
		_count = 0;
		// 清空交割单数组和索引
		memset(arrayJiaoGe, 0x00, sizeof(SJIAOGE) * _maxCount);
		memset(_idx_jiaoge_O00, 0x00, sizeof(int) * _maxCount);
		memset(_idx_jiaoge_T12, 0x00, sizeof(int) * _maxCount);
		memset(_idx_filter_jiaoge_O00, 0x00, sizeof(int) * _maxCount);
		memset(_idx_filter_jiaoge_T12, 0x00, sizeof(int) * _maxCount);
	}
	SJIAOGE * array(){
		return arrayJiaoGe;
	}
	SJIAOGE * data(int index){
		if(index >= 0 && index < _maxCount){
			return &(arrayJiaoGe[index]);
		}
		return nullptr;
	}
	boost::shared_mutex & mutex(){
		return _mutex;
	}
	int & count() {
		return _count;
	}
    SFILTER_JIAOGE & filter_jiaoge(){
        return _filter_jiaoge;
    }
    int * idx_jiaoge_O00 (){
        return _idx_jiaoge_O00;
    }
    int * idx_jiaoge_T12 (){
        return _idx_jiaoge_T12;
    }
    int *idx_filter_jiaoge_O00 (){
        return _idx_filter_jiaoge_O00;
    }
    int *idx_filter_jiaoge_T12 (){
        return _idx_filter_jiaoge_T12;
    }
    bool bIsFiltered() {
        return _bIsFiltered;
    }
    void bIsFiltered(bool filter){
        _bIsFiltered = filter;
    }
    int & iFilterCount() {
        return _iFilterCount;
    }
    boost::mutex & _index_mutexs() {
        return __index_mutexs;
    }
private:
    void init(){
        arrayJiaoGe = new SJIAOGE[_maxCount]; // 交割报单列表
        _idx_jiaoge_O00 = new int[_maxCount];  // O00索引
        _idx_jiaoge_T12 = new int[_maxCount];  // T12+O00索引
        _idx_filter_jiaoge_O00 = new int[_maxCount]; // 满足筛选O00索引
        _idx_filter_jiaoge_T12 = new int[_maxCount]; // 满足筛选T12+O00索引
		_filter_jiaoge.cI10 = -1;
		_filter_jiaoge.cO09 = -1;
		_filter_jiaoge.cO15 = -1;
		_filter_jiaoge.iM20 = -1;
		_filter_jiaoge.iM30 = -1;
    }
    void deinit(){
        if (arrayJiaoGe) {
            delete [] arrayJiaoGe;
            arrayJiaoGe = nullptr;
        }
        if (_idx_jiaoge_O00) {
            delete [] _idx_jiaoge_O00;
            _idx_jiaoge_O00 = nullptr;
        }
        if (_idx_jiaoge_T12) {
            delete [] _idx_jiaoge_T12;
            _idx_jiaoge_T12 = nullptr;
        }
        if (_idx_filter_jiaoge_O00) {
            delete [] _idx_filter_jiaoge_O00;
            _idx_filter_jiaoge_O00 = nullptr;
        }
        if (_idx_filter_jiaoge_T12) {
            delete [] _idx_filter_jiaoge_T12;
            _idx_filter_jiaoge_T12 = nullptr;
        }
    }
private:
    int  _iFilterCount = 0;
    int _maxCount = 0;
	SJIAOGE *arrayJiaoGe = nullptr;

    SFILTER_JIAOGE _filter_jiaoge;
    int *_idx_jiaoge_O00 = nullptr;  // O00索引
    int *_idx_jiaoge_T12 = nullptr;  // T12+O00索引
    int *_idx_filter_jiaoge_O00 = nullptr; // 满足筛选O00索引
    int *_idx_filter_jiaoge_T12 = nullptr; // 满足筛选T12+O00索引

    bool _bIsFiltered = false;

	boost::shared_mutex _mutex;
    boost::mutex __index_mutexs; // 索引访问锁（非需要序列化索引）
	int _count = 0;
};

class MiddleOrders {
public:
    MiddleOrders(int maxCount):_maxCount(maxCount){
        init();
    }
    ~MiddleOrders(){
        deinit();
    }
    int maxCount() const {
        return _maxCount;
    }
	void reset() {
		_count = 0;
		memset(arrayZhongLiCang, 0x00, sizeof(SZHONGLICANG) * _maxCount);
		memset(_idx_zhonglicang_O00, 0x00, sizeof(int) * _maxCount);
		memset(_idx_zhonglicang_T12, 0x00, sizeof(int) * _maxCount);
		memset(_idx_filter_zhonglicang_O00, 0x00, sizeof(int) * _maxCount);
		memset(_idx_filter_zhonglicang_T12, 0x00, sizeof(int) * _maxCount);
	}
	SZHONGLICANG * array(){
		return arrayZhongLiCang;
	}
	SZHONGLICANG * data(int index){
		if(index >= 0 && index < _maxCount){
			return &(arrayZhongLiCang[index]);
		}
		return nullptr;
	}
	boost::shared_mutex & mutex(){
		return _mutex;
	}
	int & count() {
		return _count;
	}
    SFILTER_ZHONGLICANG  & filter_zhonglicang(){
        return _filter_zhonglicang;
    }
    int * idx_zhonglicang_O00(){
        return _idx_zhonglicang_O00;
    }
    int * idx_zhonglicang_T12 (){
        return _idx_zhonglicang_T12;
    }
    int *idx_filter_zhonglicang_O00 (){
        return _idx_filter_zhonglicang_O00;
    }
    int *idx_filter_zhonglicang_T12 (){
        return _idx_filter_zhonglicang_T12;
    }
    bool bIsFiltered() {
        return _bIsFiltered;
    }
    void bIsFiltered(bool filter){
        _bIsFiltered = filter;
    }
    int & iFilterCount() {
        return _iFilterCount;
    }
    boost::mutex & _index_mutexs() {
        return __index_mutexs;
    }
private:
    void init(){
        arrayZhongLiCang = new SZHONGLICANG[_maxCount]; // 中立仓报单列表
        _idx_zhonglicang_O00 = new int[_maxCount];  // O00索引
        _idx_zhonglicang_T12 = new int[_maxCount];  // T12+O00索引
        _idx_filter_zhonglicang_O00 = new int[_maxCount]; // 满足筛选O00索引
        _idx_filter_zhonglicang_T12 = new int[_maxCount]; // 满足筛选T12+O00索引
		_filter_zhonglicang.cI10 = -1;
		_filter_zhonglicang.cO09 = -1;
		_filter_zhonglicang.cO15 = -1;
		_filter_zhonglicang.iM20 = -1;
		_filter_zhonglicang.iM30 = -1;
    }
    void deinit(){
        if (arrayZhongLiCang) {
            delete [] arrayZhongLiCang;
            arrayZhongLiCang = nullptr;
        }
        if (_idx_zhonglicang_O00) {
            delete [] _idx_zhonglicang_O00;
            _idx_zhonglicang_O00 = nullptr;
        }
        if (_idx_zhonglicang_T12) {
            delete [] _idx_zhonglicang_T12;
            _idx_zhonglicang_T12 = nullptr;
        }

        if (_idx_filter_zhonglicang_O00) {
            delete [] _idx_filter_zhonglicang_O00;
            _idx_filter_zhonglicang_O00 = nullptr;
        }
        if (_idx_filter_zhonglicang_T12) {
            delete [] _idx_filter_zhonglicang_T12;
            _idx_filter_zhonglicang_T12 = nullptr;
        }
    }
private:
    int  _iFilterCount = 0;
    int _maxCount = 0;
	SZHONGLICANG *arrayZhongLiCang = nullptr;
    SFILTER_ZHONGLICANG _filter_zhonglicang;
    int *_idx_zhonglicang_O00 = nullptr;  // O00索引
    int *_idx_zhonglicang_T12 = nullptr;  // T12+O00索引
    int *_idx_filter_zhonglicang_O00 = nullptr; // 满足筛选O00索引
    int *_idx_filter_zhonglicang_T12 = nullptr; // 满足筛选T12+O00索引

    bool _bIsFiltered = false;

	boost::shared_mutex _mutex;
    boost::mutex __index_mutexs; // 索引访问锁（非需要序列化索引）
	int _count = 0;
};

class DeliveryContractNotes {
public:
    DeliveryContractNotes(int maxCount):_maxCount(maxCount){
        init();
    }
    ~DeliveryContractNotes(){
        deinit();
    }
    int maxCount() const {
        return _maxCount;
    }
	void reset(){
		_count = 0;
		memset(arrayJgZlcChengJiaoDan, 0x00, sizeof(SJG_ZLC_CHENGJIAODAN) * _maxCount);
		memset(_idx_jg_zlc_chengjiaodan_O60_O02, 0x00, sizeof(int) * _maxCount);
		memset(_idx_filter_jg_chengjiaodan_O60_O02, 0x00, sizeof(int) * _maxCount);
		memset(_idx_filter_jg_chengjiaodan_O62, 0x00, sizeof(int) * _maxCount);
		memset(_idx_filter_zlc_chengjiaodan_O60_O02, 0x00, sizeof(int) * _maxCount);
		memset(_idx_filter_zlc_chengjiaodan_O62, 0x00, sizeof(int) * _maxCount);
	}
	SJG_ZLC_CHENGJIAODAN * array(){
		return arrayJgZlcChengJiaoDan;
	}
	SJG_ZLC_CHENGJIAODAN * data(int index){
		if(index >= 0 && index < _maxCount){
			return &(arrayJgZlcChengJiaoDan[index]);
		}
		return nullptr;
	}
	boost::shared_mutex & mutex(){
		return _mutex;
	}
	int & count() {
		return _count;
	}

    SFILTER_JG_ZLC_CHENGJIAODAN & filter_jg_chengjiaodan(){
        return _filter_jg_chengjiaodan;
    }
    SFILTER_JG_ZLC_CHENGJIAODAN & filter_zlc_chengjiaodan(){
        return _filter_zlc_chengjiaodan;
    }
    int *idx_jg_zlc_chengjiaodan_O60_O02 (){
        return _idx_jg_zlc_chengjiaodan_O60_O02;
    }
    int *idx_filter_jg_chengjiaodan_O60_O02 (){
        return _idx_filter_jg_chengjiaodan_O60_O02;;;
    }
    int *idx_filter_jg_chengjiaodan_O62 (){
        return _idx_filter_jg_chengjiaodan_O62;
    }
    int *idx_filter_zlc_chengjiaodan_O60_O02 (){
        return _idx_filter_zlc_chengjiaodan_O60_O02;
    }
    int *idx_filter_zlc_chengjiaodan_O62 (){
        return _idx_filter_zlc_chengjiaodan_O62;
    }
    bool bIsFiltered() {
        return _bIsFiltered;
    }
    void bIsFiltered(bool filter){
        _bIsFiltered = filter;
    }
    int & iFilterCountMiddle() {
        return _iFilterCountMiddle;
    }
    int & iFilterCountDelivery() {
        return _iFilterCountDelivery;
    }
    boost::mutex & _index_mutexsDelivery() {
        return __index_mutexsDelivery;
    }
    boost::mutex & _index_mutexsMiddle() {
        return __index_mutexsMiddle;
    }

private:
    void init(){
        arrayJgZlcChengJiaoDan = new SJG_ZLC_CHENGJIAODAN[_maxCount]; // 交割、中立仓成交单列表
        _idx_jg_zlc_chengjiaodan_O60_O02 = new int[_maxCount];  // O60+O02索引
        _idx_filter_jg_chengjiaodan_O60_O02 = new int[_maxCount]; // 满足筛选O60+O02索引
        _idx_filter_jg_chengjiaodan_O62 = new int[_maxCount];     // 满足筛选O62+O60+O02索引
        _idx_filter_zlc_chengjiaodan_O60_O02 = new int[_maxCount];// 满足筛选O60+O02索引
        _idx_filter_zlc_chengjiaodan_O62 = new int[_maxCount];    // 满足筛选O62+O60+O02索引

		_filter_jg_chengjiaodan.cO05 = O05_JIAOGE;
		_filter_jg_chengjiaodan.cI10 = -1;
		_filter_jg_chengjiaodan.iM20 = -1;
		_filter_jg_chengjiaodan.iM30 = -1;

		_filter_zlc_chengjiaodan.cO05 = O05_ZHONGLICANG;
		_filter_zlc_chengjiaodan.cI10 = -1;
		_filter_zlc_chengjiaodan.iM20 = -1;
	    _filter_zlc_chengjiaodan.iM30 = -1;
    }
    void deinit(){
        if (arrayJgZlcChengJiaoDan) {
            delete [] arrayJgZlcChengJiaoDan;
            arrayJgZlcChengJiaoDan = nullptr;
        }
        if (_idx_jg_zlc_chengjiaodan_O60_O02) {
            delete [] _idx_jg_zlc_chengjiaodan_O60_O02;
            _idx_jg_zlc_chengjiaodan_O60_O02 = nullptr;
        }
        if (_idx_filter_jg_chengjiaodan_O60_O02) {
            delete [] _idx_filter_jg_chengjiaodan_O60_O02;
            _idx_filter_jg_chengjiaodan_O60_O02 = nullptr;
        }
        if (_idx_filter_jg_chengjiaodan_O62) {
            delete [] _idx_filter_jg_chengjiaodan_O62;
            _idx_filter_jg_chengjiaodan_O62 = nullptr;
        }
        if (_idx_filter_zlc_chengjiaodan_O60_O02) {
            delete [] _idx_filter_zlc_chengjiaodan_O60_O02;
            _idx_filter_zlc_chengjiaodan_O60_O02 = nullptr;
        }
        if (_idx_filter_zlc_chengjiaodan_O62) {
            delete [] _idx_filter_zlc_chengjiaodan_O62;
            _idx_filter_zlc_chengjiaodan_O62 = nullptr;
        }
    }
private:
    int  _iFilterCountMiddle = 0;
    int  _iFilterCountDelivery = 0;
    int _maxCount = 0; 
	SJG_ZLC_CHENGJIAODAN *arrayJgZlcChengJiaoDan = nullptr;

    SFILTER_JG_ZLC_CHENGJIAODAN _filter_jg_chengjiaodan;
    SFILTER_JG_ZLC_CHENGJIAODAN _filter_zlc_chengjiaodan;
    int *_idx_jg_zlc_chengjiaodan_O60_O02 = nullptr;  // O60+O02索引
    int *_idx_filter_jg_chengjiaodan_O60_O02 = nullptr; // 满足筛选O60+O02索引
    int *_idx_filter_jg_chengjiaodan_O62 = nullptr;     // 满足筛选O62+O60+O02索引
    int *_idx_filter_zlc_chengjiaodan_O60_O02 = nullptr;// 满足筛选O60+O02索引
    int *_idx_filter_zlc_chengjiaodan_O62 = nullptr;    // 满足筛选O62+O60+O02索引

    bool _bIsFiltered = false; //TODO: ?????  useless ????

	boost::shared_mutex _mutex;
    boost::mutex __index_mutexsDelivery; // 索引访问锁（非需要序列化索引）
    boost::mutex __index_mutexsMiddle; // 索引访问锁（非需要序列化索引）
	int _count = 0; //TODO: ???? error
};

class Notices {
public:
    Notices(int maxCount):_maxCount(maxCount){
        init();
    }
    ~Notices(){
        deinit();
    }
    int maxCount() const {
        return _maxCount;
    }
	void reset(){
		_count = 0;
		memset(arrayGongGao, 0x00, sizeof(SGONGGAO) * _maxCount);
		memset(_idx_gonggao_T80, 0x00, sizeof(int) * _maxCount);
	}
	SGONGGAO * array(){
		return arrayGongGao;
	}
	SGONGGAO * data(int index){
		if(index >= 0 && index < _maxCount){
			return &(arrayGongGao[index]);
		}
		return nullptr;
	}
	boost::shared_mutex & mutex(){
		return _mutex;
	}
	int & count() {
		return _count;
	}
    SFILTER_GONGGAO & filter_gonggao(){
        return _filter_gonggao;
    }
    int *idx_gonggao_T80 (){
        return _idx_gonggao_T80;
    }
    bool bIsFiltered() {
        return _bIsFiltered;
    }
    void bIsFiltered(bool filter){
        _bIsFiltered = filter;
    }
    int & iFilterCount() {
        return _iFilterCount;
    }
    boost::mutex & _index_mutexs() {
        return __index_mutexs;
    }
private:
	void init(){
		arrayGongGao = new SGONGGAO[_maxCount];
		_idx_gonggao_T80 = new int[_maxCount];
	}
	void deinit(){
		if(arrayGongGao != nullptr){
			delete [] arrayGongGao;
			arrayGongGao = nullptr;
		}
		if(_idx_gonggao_T80 != nullptr){
			delete [] _idx_gonggao_T80;
			_idx_gonggao_T80 = nullptr;
		}
	}
private:
    int  _iFilterCount = 0;
    int _maxCount = 0; 
	SGONGGAO * arrayGongGao = nullptr; // 公告列表

    SFILTER_GONGGAO _filter_gonggao;
    int *_idx_gonggao_T80 = nullptr; // T80索引

    bool _bIsFiltered = false;

	boost::shared_mutex _mutex;
    boost::mutex __index_mutexs; // 索引访问锁（非需要序列化索引）
	int _count = 0;
};


#endif /*MEMORYDATAINITDEINIT_H_ */
